<template>
  <view class="w-full h-100vh bg-f5f5f5 flex flex-col align-items-center">
    <image :src="`${$oss}/static/ai/01.png`" class="w-400 h-400 radius-circle" style="margin-top: 15vh;"></image>
    <view class="auth-btn w-600 h-80 radius-52 text-white f-40 flex-center" @click="openAuth">授权登录</view>

    <!-- 	<wd-button custom-class="custom-shadow" open-type="getPhoneNumber"
      @getphonenumber="getMobile">授权登录</wd-button> -->

  </view>
</template>

<script setup>
import {useUserStore} from '@/store/user.js'

const {api_get_token, api_get_myInfo} = useRequest()
const {proxy} = getCurrentInstance()
const userStore = useUserStore()
// 获取手机号-暂未使用
const getMobile = (e) => {
  if (e.errMsg == 'getPhoneNumber:ok') {
    let params = {
      encryptedData: e.encryptedData,
      iv: e.iv
    }
    api_get_phone(params)
        .then(res => {
          formData.value.mobile = res.mobile
        }).catch(() => {
    })
  }
}

// 授权登录
const openAuth = async () => {
  const loginRes = await uni.login()
  const res = await api_get_token({
    code: loginRes.code,
    scene: proxy.$storage.has('scene') ? proxy.$storage.get('scene') : ''
  })
  if (res.code === 0) {
    if(proxy.$storage.has('scene')) {
      proxy.$storage.remove('scene')
    }
    proxy.$storage.set('token', res.data.accessToken)
    api_get_myInfo()
    uni.showToast({
      title: '登录成功',
      icon: 'success'
    })

    setTimeout(() => {
      uni.navigateBack()
    }, 2000)
  }
}
</script>

<style lang="scss" scoped>
.auth-btn {
  background-color: #6275ff;
  margin: 80rpx auto;
}
</style>